<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone" width="220">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号" prop="sno" width="180">
        <el-input
          v-model="queryParams.sno"
          placeholder="请输入学号或教师编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证状态" prop="approveStatus" width="220">
        <el-select v-model="queryParams.approveStatus" placeholder="请选择认证状态: 0-未认证;1-认证中;2-认证成功;3-认证失败" clearable size="small">
          <el-option
            v-for="dict in approveStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账号状态" prop="isEnable" width="220">
        <el-select v-model="queryParams.isEnable" placeholder="请选择账号状态" clearable size="small" v-if="isEnableOptions.length>0">
          <el-option
            v-for="dict in isEnableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['member:member:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:member:remove']"
        >删除</el-button>
      </el-col> -->
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange" size="small">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="头像URL" align="center" width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 60px; height: 60px"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phone" width="150"/>
      <el-table-column label="学号或教师编号" align="center" prop="sno" width="150"/>
      <el-table-column label="学院" align="center" prop="college" />
      <el-table-column label="专业" align="center" prop="specialty" />
      <el-table-column label="哪一届" align="center" prop="step" />
      <el-table-column label="认证图片" align="center" width="160">
        <template slot-scope="scope">
          <el-image
            style="width: 130px; height: 100px"
            :lazy="true"
            :src="scope.row.approveImg"
            :preview-src-list="[scope.row.approveImg]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="认证状态" align="center" prop="approveStatus" :formatter="approveStatusFormat" />
      <el-table-column label="是否启用" align="center" prop="isEnable" :formatter="isEnableFormat" />
      <el-table-column label="最后登录IP地址" align="center" prop="lastLoginIp" width="130"/>
      <el-table-column label="累计登录次数" align="center" prop="loginCount" width="130"/>
      <el-table-column label="最后登录时间" align="center" prop="lastLoginTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            style="padding: 4px 6px;"
            v-if="scope.row.isEnable==1"
            type="danger"
            @click="enableClick(scope.row, 0)"
            v-hasPermi="['member:member:edit']"
          >禁用</el-button>
          <el-button
            size="mini"
            style="padding: 4px 6px;"
            v-else
            type="success"
            @click="enableClick(scope.row, 1)"
            :disabled="true"
            v-hasPermi="['member:member:edit']"
          >启用</el-button>

          <el-button
            size="mini"
            v-if="scope.row.approveStatus==0"
            style="padding: 4px 6px;"
            type="warning"
            :disabled="true"
            v-hasPermi="['member:member:remove']"
          >待认证</el-button>
          <el-button
            size="mini"
            style="padding: 4px 6px;"
            v-else-if="scope.row.approveStatus==1"
            type="danger"
            @click="openAudit(scope.row.id)"
            v-hasPermi="['member:member:remove']"
          >待审核</el-button>
          <el-button
            size="mini"
            style="padding: 4px 6px;"
            v-else
            type="success"
            :disabled="true"
            v-hasPermi="['member:member:remove']"
          >已审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像URL" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像URL" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="学号或教师编号" prop="sno">
          <el-input v-model="form.sno" placeholder="请输入学号或教师编号" />
        </el-form-item>
        <el-form-item label="学院" prop="college">
          <el-input v-model="form.college" placeholder="请输入学院" />
        </el-form-item>
        <el-form-item label="专业" prop="specialty">
          <el-input v-model="form.specialty" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="哪一届" prop="step">
          <el-input v-model="form.step" placeholder="请输入哪一届" />
        </el-form-item>
        <el-form-item label="认证图片" prop="approveImg">
          <el-input v-model="form.approveImg" placeholder="请输入认证图片" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="form.isEnable">
            <el-radio
              v-for="dict in isEnableOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 审核是否通过信息对话框 -->
    <el-dialog title="操作" :visible.sync="open2" width="320px" height="240px" append-to-body>
      <el-select v-model="auditVal" placeholder="请选择是否通过" clearable size="medium">
        <el-option
          label="通过"
          :value="1"
        />
        <el-option
          label="不通过"
          :value="0"
        />
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="auditCommit()">确 定</el-button>
        <el-button @click="open2=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember, exportMember } from "@/api/member/user";

export default {
  name: "Member",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户基本信息表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      auditVal: null,
      // 认证状态: 0-未认证;1-认证中;2-认证成功;3-认证失败字典
      approveStatusOptions: [],
      // 是否启用，1=启用，0=未启用字典
      isEnableOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: null,
        phone: null,
        sno: null,
        approveStatus: null,
        isEnable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickname: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        avatar: [
          { required: true, message: "头像URL不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        isEnable: [
          { required: true, message: "是否启用，1=启用，0=未启用不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_approve_status").then(response => {
      this.approveStatusOptions = response.data;
    });
    this.getDicts("sys_user_enable_status").then(response => {
      this.isEnableOptions = response.data;
    });
  },
  methods: {
    /** 查询用户基本信息列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 认证状态: 0-未认证;1-认证中;2-认证成功;3-认证失败字典翻译
    approveStatusFormat(row, column) {
      return this.selectDictLabel(this.approveStatusOptions, row.approveStatus);
    },
    // 是否启用，1=启用，0=未启用字典翻译
    isEnableFormat(row, column) {
      return this.selectDictLabel(this.isEnableOptions, row.isEnable);
    },
    // 获取字典文本
    selectDictLabel(options, value){
      return options[value].dictLabel
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        password: null,
        nickname: null,
        avatar: null,
        phone: null,
        email: null,
        weixinOpenid: null,
        sessionKey: null,
        sno: null,
        college: null,
        specialty: null,
        step: null,
        approveImg: null,
        approveStatus: null,
        isEnable: 0,
        lastLoginIp: null,
        loginCount: null,
        lastLoginTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户基本信息";
      });
    },
    enableClick(row, status){
      this.reset();
      this.form.id = row.id
      this.form.isEnable = status
      updateMember(this.form).then(response => {
        this.$message({
          message: `账号${status==1?'启用':'禁用'}成功`,
          type: 'success'
        });
        this.getList();
      });
    },
    auditCommit(){
      if(this.auditVal == null || this.auditVal == ''){
        this.$message.error('您还没有选择操作哦');
        return
      }
      let id = this.form.id
      this.reset();
      this.form.id = id
      this.form.isEnable = 1
      if(this.auditVal == 1){
        this.form.approveStatus = 2
      }else{
        this.form.approveStatus = 3
      }
      updateMember(this.form).then(response => {
        this.$message({
          message: '审核成功',
          type: 'success'
        });
        this.open2 = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    openAudit(id){
      this.form.id = id
      this.open2 = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户基本信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户基本信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMember(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
